package org.chen.wordwave.act;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson2.JSON;
import com.hjq.toast.ToastParams;
import com.hjq.toast.Toaster;
import com.hjq.toast.style.CustomToastStyle;

import org.chen.wordwave.Api.ApiClient;
import org.chen.wordwave.Api.ApiService;
import org.chen.wordwave.R;
import org.chen.wordwave.base.BaseActivity;
import org.chen.wordwave.bean.FindPws;
import org.chen.wordwave.init.WordInit;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ForgotPasswordOnline extends BaseActivity {
    private ApiService apiService;
    private EditText etEmail;
    private EditText etUsername;
    private TextView question;
    private Button btnResetPassword;
    private ToastParams toastParams = new ToastParams();
    private ProgressDialog progressDialog;
    @Override
    public Integer getLayoutResId() {
        return R.layout.forgot_password_online_act;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        apiService = ApiClient.getClient(getApplicationContext()).create(ApiService.class);
    }

    @Override
    public void init() {
        etEmail = findViewById(R.id.et_email);
        etUsername = findViewById(R.id.et_username);
        btnResetPassword = findViewById(R.id.btn_reset_password);
        question = findViewById(R.id.question);

        btnResetPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleResetPassword();
            }
        });

        question.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
                emailIntent.setData(Uri.parse("mailto:xchen6367@gmail.com")); // 设置开发者的电子邮件地址
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.wordmaster_email)); // 设置电子邮件主题
                if (emailIntent.resolveActivity(getPackageManager()) != null) {
                    startActivity(emailIntent);
                }
            }
        });
    }

    private void handleResetPassword() {
        showProgressDialog();
        String email = etEmail.getText().toString().trim();
        String username = etUsername.getText().toString().trim();

        if (TextUtils.isEmpty(email) || TextUtils.isEmpty(username)) {
            Toaster.show(getString(R.string.find_pleas));
            dismissProgressDialog();
            return;
        }

        try {
            sendResetPasswordRequest(email, username);
        } catch (Exception e) {
            Log.e("Error occurred while encrypting data: ", e.toString());
            dismissProgressDialog();
        }
    }
    private void sendResetPasswordRequest(String email,String username) throws Exception {

        FindPws findPws = new FindPws();
        findPws.setEmail(email);
        findPws.setAccount(username);
        String language = WordInit.language;
        if (language.equals("zh")) {
            // 中文
            findPws.setLanguage("zh");
        } else {
            // 英文
            findPws.setLanguage("en");
        }
        apiService.forGetUserPws(findPws).enqueue(new Callback<String>() {
            @Override
            public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
                if (response.isSuccessful() && response.body() != null){
                    if (response.body().equals("200")){
                        toastParams.style = new CustomToastStyle(R.layout.success);
                        toastParams.text = getString(R.string.send_find_pws);
                        Toaster.show(toastParams);
                    }else {
                        toastParams.style = new CustomToastStyle(R.layout.error);
                        toastParams.text = getString(R.string.send_fail_email_or_account);
                        Toaster.show(toastParams);
                    }
                }else {
                    toastParams.style = new CustomToastStyle(R.layout.error);
                    toastParams.text = getString(R.string.send_fail);
                    Toaster.show(toastParams);
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailure(@NonNull Call<String> call,@NonNull Throwable t) {
                toastParams.style = new CustomToastStyle(R.layout.error);
                toastParams.text = getString(R.string.network_error);
                Toaster.show(toastParams);
                dismissProgressDialog();
            }
        });
    }
    private void showProgressDialog() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage(getString(R.string.uploading));
            progressDialog.setCancelable(false);
        }
        progressDialog.show();
    }

    private void dismissProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }
}
